// Created by WXX on 2021/8/26 9:39
#include <iostream>
#include <vector>
#include <algorithm>

using namespace std;

/**
 * 执行用时：84 ms, 在所有 C++ 提交中击败了50.68%的用户
 * 内存消耗：41 MB, 在所有 C++ 提交中击败了8.74%的用户
 */
class Solution {
public:
    int numRescueBoats(vector<int>& people, int limit) {
        sort(people.begin(), people.end());

        int res = 0;
        for (int i = people.size() - 1, j = 0; j <= i; i--) {
            if (j < i && people[i] + people[j] <= limit) j++;
            res++;
        }
        return res;
    }
};

int main() {

    vector<int> people = {3, 2, 2, 1};
    cout << Solution().numRescueBoats(people, 3) << endl;  // 3

    return 0;
}
